package com.ditri.distrident.commonservice.controller;

import com.ditri.distrident.common.constant.ErrorCodeEnum;
import com.ditri.distrident.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.xml.MappingJackson2XmlView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: edxuanlen
 * @date: 2020-08-03 11:28
 * @version: 1.0
 **/
@Slf4j
public class BaseController {
    /**
     * 入参校验统一处理.
     *
     * @param bindingResult 异常处理对象
     */
    public void validateOperator(BindingResult bindingResult) {
//		if (null != bindingResult && bindingResult.hasFieldErrors()) {
//			List<FieldError> errors = bindingResult.getFieldErrors();
//			String errorCode = "";
//			String errorMsg = "";
//			for (FieldError error : errors) {
//				errorCode = ErrorCodeEnum
//						.getErrorCodeByMsg(error.getDefaultMessage());
//				if (errorCode.equals(ErrorCodeEnum.NOT_EMPTY.getCode())) {
//					errorMsg = error.getDefaultMessage()
//							.replaceFirst("\\{[^}]*\\}", error.getField());
//					break;
//				} else {
//					errorMsg = error.getDefaultMessage()
//							.replaceFirst("\\{[^}]*\\}", error.getField());
//				}
//			}
//			throw new ValidationException(errorCode, errorMsg);
//		}
    }


    /**
     * 〈一句话功能简述〉 返回错误码.
     * 〈功能详细描述〉.
     *
     * @param errorCode 错误码
     * @param errorMsg  错误信息描述
     * @param result    Result
     * @return Map<String, Result>
     */
    public static Map<String, Result> createErrorResult(String errorCode,
                                                        String errorMsg, Result result) {
        Map<String, Result> resultMap = new HashMap<>();
        if (errorCode.startsWith("20000") || errorCode.startsWith("30000") || errorCode.startsWith("20200")) {
            result.setResultMessage(errorMsg);
        } else if (errorCode.startsWith("16")) {
//	FIXME		result.setMsgForQueryUserInfo(errorMsg);
        } else {
            result.setResultMessage(errorMsg);
        }
        result.setResultCode(errorCode);
        resultMap.put("result", result);
        return resultMap;
    }
}

